﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using PropertyChanged;
using System;
using System.ComponentModel.Composition;
using PongChong.Fun.BingWall;
using System.Windows.Threading;
using System.Threading.Tasks;
using System.Linq;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Media.Imaging;


namespace BingWallPaper.ViewModels
{
    [Export]
    [ImplementPropertyChanged]
    public class MainViewModel : ViewModelBase
    {
        public string Title { get; set; }
        public Image ImageObject { get; set; }
        public string LastImageUriString { get; set; }
        public System.Windows.Controls.Image Image1 { get; set; }
        public System.Windows.Controls.Image Image2 { get; set; }
        public bool Downloading { get; set; }

        public MainViewModel()
        {
            Image1 = new System.Windows.Controls.Image();
            Image1.Stretch = System.Windows.Media.Stretch.UniformToFill;
            Image2 = new System.Windows.Controls.Image();
            Image2.Stretch = System.Windows.Media.Stretch.UniformToFill;
            Image1.SnapsToDevicePixels = true;
            Image2.SnapsToDevicePixels = true;

            NextCommand.Execute(null);
        }

        private RelayCommand _nextCommand;
        public RelayCommand NextCommand
        {
            get
            {
                return _nextCommand ?? (_nextCommand = new RelayCommand(() =>
                 {
                     Downloading = true;

                     Task.Factory.StartNew(() =>
                     {
                         LastImageUriString = ImageObject == null ? "" : ImageObject.url;
                         ImageObject = BingWall.GetRandomImage().images.First();

                     }).ContinueWith(r =>
                     {
                         Invoke(() =>
                         {
                             var img = new BitmapImage();
                             img.BeginInit();

                             if (!ImageObject.url.StartsWith("http://s.cn.bing.net"))
                                 ImageObject.url = "http://s.cn.bing.net" + ImageObject.url;

                             img.UriSource = new Uri(ImageObject.url);
                             img.EndInit();

                             string u1 = Image1.Source == null ? "" : ((BitmapImage)Image1.Source).UriSource.AbsoluteUri;
                             string u2 = Image2.Source == null ? "" : ((BitmapImage)Image2.Source).UriSource.AbsoluteUri;

                             if (u1.Equals(LastImageUriString))
                             {
                                 Image2.Source = img;
                                 Messenger.Default.Send(new SetContent { Image = Image2 });
                             }
                             else
                             {
                                 Image1.Source = img;
                                 Messenger.Default.Send(new SetContent { Image = Image1 });
                             }

                         });

                         Downloading = false;
                     });

                 }));
            }
        }

        private RelayCommand _setCommand;
        public RelayCommand SetCommand
        {
            get
            {
                return _setCommand ?? (_setCommand = new RelayCommand(() =>
                {
                    BingWall.SetWallpaper(new Uri(ImageObject.url), Wallpaper.Style.Stretched);
                }));
            }
        }

        private RelayCommand _settingsCommand;
        public RelayCommand SettingsCommand
        {
            get
            {
                return _settingsCommand ?? (_settingsCommand = new RelayCommand(() =>
                {
                    //自动换壁纸时间
                    //关闭按钮点击行为
                    //开机启动


                }));
            }
        }

        private RelayCommand _saveCommand;
        public RelayCommand SaveCommand
        {
            get
            {
                return _saveCommand ?? (_saveCommand = new RelayCommand(() =>
                {
                    Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                    dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                    dlg.Filter = "JPG文件(*.jpg)|*.jpg|PNG文件(*.png)|*.png";
                    if ((bool)dlg.ShowDialog())
                    {
                        bool successed = BingWall.DownloadImage(ImageObject.url, dlg.FileName);
                    }
                }
                , () =>
                {
                    return ImageObject != null;
                }
                ));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        public static void Invoke(Action action)
        {
            Dispatcher dispatchObject = System.Windows.Application.Current.Dispatcher;
            if (dispatchObject == null || dispatchObject.CheckAccess())
            {
                action();
            }
            else
            {
                dispatchObject.BeginInvoke(action);
            }
        }

    }
}
